package at.sysarch.filter;

import java.awt.image.renderable.ParameterBlock;
import java.util.LinkedList;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.operator.MedianFilterDescriptor;
import javax.media.jai.operator.MedianFilterShape;

import at.sysarch.utils.ImageChangedEvent;
import at.sysarch.utils.ImageChangedListener;

public class MedianFilter extends AbstractListenerManager implements ImageChangedListener {

	PlanarImage _image = null;
	private int _maskSize = 10;
	private MedianFilterShape _maskShape = MedianFilterDescriptor.MEDIAN_MASK_SQUARE;
	LinkedList<ImageChangedListener> listeners = new LinkedList<ImageChangedListener>();

	public void setInputImage(PlanarImage image) {
		_image = image;
		updateImage();
	}

	@Override
	public void imageChanged(ImageChangedEvent event) {
		setInputImage(event.getImage());
	}

	private void updateImage() {
		ParameterBlock medianParams = new ParameterBlock();
		medianParams.addSource(_image);
		medianParams.add(_maskShape);
		medianParams.add(_maskSize);

		PlanarImage image = JAI.create("MedianFilter", medianParams);

		fireImageChanged(image);
	}

	public void setMaskSize(int maskSize) {

		_maskSize = maskSize;
		System.out.println("mask sie:  "+ maskSize);;
		updateImage();
	}

	public int getMaskSize() {
		return _maskSize;
	}

	public void setMaskShape(MedianFilterShape maskShape) {

		_maskShape = maskShape;
		updateImage();
	}

	public MedianFilterShape getMaskShape() {

		return _maskShape;
	}

}
